package com.example.app.pages.main.home.addNew.addRecord.MyModalBottomSheetRelationPicker

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.width
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.ManageAccounts
import androidx.compose.material3.ButtonColors
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ListItemDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.example.app.addNewViewModel
import com.example.app.components.MyButton.MyFilledTonalButton
import com.example.app.components.MyDivider.MyDivider
import com.example.app.components.MyDivider.MyDividerDirection
import com.example.app.components.MyIcon.MyIcon
import com.example.app.components.MyIcon.MyNavigationIcon
import com.example.app.components.MyListItem.MyListItem
import com.example.app.components.MyScaffold.MyCustomScaffold
import com.example.app.components.MySpacer.MySpacer
import com.example.app.components.MyText.MyText
import com.example.app.components.MyTextField.MyTextField
import com.example.app.components.MyTextField.MyTextFieldState
import com.example.app.components.MyTopBar.MyTopBar
import com.example.app.data.bean.routeAddAccount.AccountRelationPeople
import com.example.app.pages.main.home.me.viewModel.AddNewViewModel
import com.example.app.pages.main.mainTipState
import com.example.app.ui.theme.smallSpacer
import com.example.app.viewmodel.MainViewModel
import kotlinx.coroutines.launch

var editRelationTextFieldState = MyTextFieldState()
//修改关系人
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun EditRelation(
        curRelationPeople: AccountRelationPeople,

        animateOffsetX: Dp,

        dismiss: () -> Unit,
        editFinishHandler: () -> Unit,
) {
    var windowWidth = with(LocalDensity.current) {
        LocalConfiguration.current.screenWidthDp
    }.dp
    MyCustomScaffold(
        modifier = Modifier
            .offset(x = animateOffsetX)
            .width(windowWidth)
            .fillMaxHeight()
            .background(MaterialTheme.colorScheme.background),
        topBar = {
            MyTopBar(title = {
                MyText(text = "修改")
            },
                windowInsets = WindowInsets(
                    0,
                    0,
                    0,
                    0
                ),
                actions = {},
                navigationIcon = {
                    MyNavigationIcon(onClick = {
                        dismiss()
                    })
                })
        },
        topBarDivider = {
            MyDivider(myDividerDirection = MyDividerDirection.HOR)
        },
        content = {
            var scope = rememberCoroutineScope()
            var relationName = editRelationTextFieldState.textFieldValue.collectAsState()
            var relationNameError = editRelationTextFieldState.isError.collectAsState()
            //初始化
            editRelationTextFieldState.flowUpdate_textFieldValue(TextFieldValue(text = curRelationPeople.Title.toString()))
            Column {
                MySpacer(modifier = Modifier.height(smallSpacer))
                MySpacer(modifier = Modifier.height(smallSpacer))
                MyListItem(tonalElevation = 0.dp,
                    shadowElevation = 0.dp,
                    colors = ListItemDefaults.colors(containerColor = MaterialTheme.colorScheme.background),
                    headlineContent = {
                        MyTextField(
                            myTextFieldState = editRelationTextFieldState,
                            leadingIcon = {
                                MyIcon(imageVector = Icons.Rounded.ManageAccounts)
                            },
                            placeholder = {
                                MyText(text = "请输入关系人名称")
                            },
                            value = relationName.value,
                            onValueChange = {
                                editRelationTextFieldState.flowUpdate_textFieldValue(it)
                            },
                            label = {
                                MyText(text = "关系人名称")
                            },
                            isError = relationNameError.value
                        )
                    })
                MySpacer(modifier = Modifier.height(smallSpacer))
                MyListItem(tonalElevation = 0.dp,
                    shadowElevation = 0.dp,
                    colors = ListItemDefaults.colors(containerColor = MaterialTheme.colorScheme.background),
                    headlineContent = {
                        MyFilledTonalButton(enabled = !relationName.value.text.isEmpty(),
                            colors = ButtonColors(
                                containerColor = MaterialTheme.colorScheme.surfaceTint,
                                contentColor = MaterialTheme.colorScheme.onPrimary,
                                disabledContainerColor = ButtonDefaults.buttonColors().disabledContainerColor,
                                disabledContentColor = ButtonDefaults.buttonColors().disabledContentColor
                            ),
                            modifier = Modifier.fillMaxWidth(),
                            onClick = {
                                scope.launch {
                                    var result =
                                        addNewViewModel.request_editAccountRelationPeople(
                                            Title = relationName.value.text,
                                            _id = curRelationPeople._id.toString()
                                        )
                                            .await()
                                    if (result.code < 1) {
                                        mainTipState.show(description = "添加成功！")
                                        editRelationTextFieldState.flowUpdate_textFieldValue(TextFieldValue(""))
                                        addNewViewModel.updateAccountRelationPeopleList()
                                        dismiss()
                                    }
                                    editFinishHandler()
                                }
                            }) {
                            MyText(
                                text = "保存",
                                fontWeight = FontWeight.W500
                            )
                        }
                    })
            }
        })



}